import { getMajorTree, submitNormalStudentAuthApply, submitTeacherAuthApplyDto } from '../../api/user'
import { listByKind } from '../../api/base'
import { wxUpload, uploadFileName } from '../../utils/upload'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageHeight: 0,
    identity: 'student',

    teacherList: [
      { code: 'PRIMARY', name: '小学教师资格证' },
      { code: 'JUNIOR_MIDDLE', name: '初中教师资格证' },
      { code: 'SENIOR_MIDDLE', name: '高中教师资格证' },
      { code: 'UNIVERSITY', name: '大学教师资格证' }
    ],
    showTeacher: false,
    selTeacher: '',
    teacherCertUrls: [],

    titleList: [
      { code: 'PRIMARY', name: '一级教师' },
      { code: 'SECONDARY', name: '二级教师' },
      { code: 'TERTIARY', name: '三级教师' },
      { code: 'SENIOR', name: '高级教师' },
      { code: 'POSITIVE_SENIOR', name: '正高级教师' },
    ],
    showTitle: false,
    selTitle: '',
    titleCertUrls: [],

    studentCertUrls: [],
    idcardFrontUrls: [],
    idcardBackUrls: [],
    zyzUrls: [],

    form: {
      idcard: {
        authApplyId: null,
        idcardBackUrl: '',
        idcardFrontUrl: '',
        idcardNumber: '',
        realName: ''
      },
      studentCert: {
        authApplyId: null,
        studentCertUrl: '',
        studentCertValid: ''
      },
      teacherCert: {
        authApplyId: null,
        teacherCertCode: '',
        teacherCertType: '',
        teacherCertUrl: ''
      },
      titleCert: {
        authApplyId: null,
        title: '',
        titleCertCode: '',
        titleCertUrl: ''
      },
      userId: 0,
      zyz: {
        authApplyId: null,
        zyzUrl: ''
      }
    },

    region: [],

    fileList: [] // 资格证
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      identity: options.key
    })
    this.setPageHeight()
    this.init()
  },

  back() {
    wx.navigateBack({ delta: 1 })
  },

  setPageHeight() {
    let windowHeight
    wx.getSystemInfo({
      success: function(res) {
        windowHeight = res.windowHeight
      }
    });
    let query = wx.createSelectorQuery().in(this);
    query.select('.info-nav-wrap').boundingClientRect();
    query.exec((res) => {
      this.setData({
        pageHeight: windowHeight - res[0].height
      })
    });
  },

  init() {

  },

  /** 选择资格证 start **/
  selectTeacher() {
    this.setData({showTeacher: true})
  },

  cancelSelectTeacher() {
    this.setData({showTeacher: false})
  },

  confirmSelectTeacher(e) {
    this.setData({
      showTeacher: false,
      selTeacher: e.detail.name,
      'form.teacherCert.teacherCertType': e.detail.code
    })
  },

  editTeacherCertCode(e) {
    this.setData({
      'form.teacherCert.teacherCertCode': e.detail.value
    })
  },
  /** 选择资格证 end **/

  /** 选择职称 start **/
  selectTitle() {
    this.setData({showTitle: true})
  },

  cancelSelectTitle() {
    this.setData({showTitle: false})
  },

  confirmSelectTitle(e) {
    this.setData({
      showTitle: false,
      selTitle: e.detail.name,
      'form.titleCert.titleCertType': e.detail.code
    })
  },

  editTitleCertCode(e) {
    this.setData({
      'form.titleCert.titleCertCode': e.detail.value
    })
  },
  /** 选择职称 end **/

  /** 选择身份证 start **/
  editIdcardNumber(e) {
    this.setData({
      'form.idcard.idcardNumber': e.detail.value
    })
  },
  editRealName(e) {
    this.setData({
      'form.idcard.realName': e.detail.value
    })
  },
  /** 选择身份证 end **/


  /** 上传图片 **/
  afterRead(event) {
    const _this = this;
    const { file } = event.detail;
    const fileName = uploadFileName(file.path);
    const name = event.currentTarget.dataset.name;

    wxUpload({
      biz: 'CERTIFICATE',
      fileName: fileName,
      size: file.size,
      filePath: file.path
    }).then(res => {
      let fileList;
      switch (name) {
        case 'teacherCert':
          fileList = [{url: res.uri}]
          _this.setData({
            teacherCertUrls: fileList,
            'form.teacherCert.teacherCertUrl': fileList[0].url
          })
          break;
        case 'titleCert':
          fileList = [{url: res.uri}]
          _this.setData({
            titleCertUrls: fileList,
            'form.titleCert.titleCertUrl': fileList[0].url
          })
          break;
        case 'idcardFront':
          fileList = [{url: res.uri}]
          _this.setData({
            idcardFrontUrls: fileList,
            'form.idcard.idcardFrontUrl': fileList[0].url
          })
          break;
        case 'idcardBack':
          fileList = [{url: res.uri}]
          _this.setData({
            idcardBackUrls: fileList,
            'form.idcard.idcardBackUrl': fileList[0].url
          })
          break;
        case 'zyz':
          fileList = [{url: res.uri}]
          _this.setData({
            zyzUrls: fileList,
            'form.zyz.zyzUrl': fileList[0].url
          })
          break;
      }
    })
  },

  removeImage(e) {
    const name = e.currentTarget.dataset.name;
    const _this = this
    switch (name) {
      case 'teacherCert':
        _this.setData({
          teacherCertUrls: [],
          'form.teacherCert.teacherCertUrl': ''
        })
        break;
      case 'titleCert':
        _this.setData({
          titleCertUrls: [],
          'form.titleCert.titleCertUrl': ''
        })
        break;
      case 'studentCert':
        _this.setData({
          studentCertUrls: [],
          'form.studentCert.studentCertUrl': ''
        })
        break;
      case 'idcardFront':
        _this.setData({
          idcardFrontUrls: [],
          'form.idcard.idcardFrontUrl': ''
        })
        break;
      case 'idcardBack':
        _this.setData({
          idcardBackUrls: [],
          'form.idcard.idcardBackUrl': ''
        })
        break;
      case 'zyz':
        _this.setData({
          zyzUrls: [],
          'form.zyz.zyzUrl': ''
        })
        break;
    }
  },

  skip() {
    wx.switchTab({
      url: '/pages/home/home'
    })
  },

  next() {
    if(this.data.identity === 'student') {
      submitNormalStudentAuthApply(this.data.form).then(res => {
        wx.switchTab({
          url: '/pages/home/home'
        })
      })
    } else {
      submitTeacherAuthApplyDto(this.data.form).then(res => {
        wx.switchTab({
          url: '/pages/home/home'
        })
      })
    }
  }
})
